// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import SdkManager from "../manager/SdkManager";
import { privacyChannel } from "../Script/privacyChannel";
import { prv } from "../Script/prv";
import { privacyChannelOppo } from "../Script/privacyChannelOppo";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ExitLayer extends BaseLayer {

    panel: cc.Node = null
    btnSubmit: cc.Node = null
    btnCancel: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnSubmit = cc.find('buttons/btn_submit', this.panel)
        this.btnCancel = cc.find('buttons/btn_cancel', this.panel)
        this.btnSubmit.on('click', this.onSubmitClick, this)
        this.btnCancel.on('click', this.onCancelClick, this)
    }
    show(): void {
        super.show()
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }
                break
        }
    }
    onDestroy() {
        this.btnSubmit.off('click', this.onSubmitClick, this)
        this.btnCancel.off('click', this.onCancelClick, this)
    }

    onEnable() {
        this.zoomIn(this.panel)
        SdkManager.instance.showInterstitialAd()
    }

    onDisable() { }

    onCancelClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.EXIT, false)
    }

    onSubmitClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataManager.instance.status = ENUM_GAME_STATUS.UNRUNING
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.EXIT, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
        switch (privacyChannel.channel) {
            case "oppo":
                if (privacyChannel.getChannel().getCtrl("p13")) {
                    privacyChannel.getChannel().showVideo(() => {

                    })
                }
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    let style = {
                        top: prv.getSysInfo().windowHeight / 2 + 270
                    };
                    prv.showInter(false, style);
                    // privacyChannel.getChannel().showInter(true, style)
                    (privacyChannel.getChannel() as privacyChannelOppo).showInterMainLayer(true, style)

                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "oppo1":

                if (privacyChannel.getChannel().getCtrl("p2")) {
                    let style = {
                        top: prv.getSysInfo().windowHeight / 2 + 270
                    };
                    prv.showInter(false, style);
                    // privacyChannel.getChannel().showInter(true, style)
                    (privacyChannel.getChannel() as privacyChannelOppo).showInterMainLayer(true, style)

                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    //prv.showInter(false, style);
                    privacyChannel.getChannel().showInter()
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "vivo":
            case "vivo1":
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    let style = {
                        top: prv.getSysInfo().windowHeight / 2 + 270
                    };
                    //prv.showInter(false, style);
                    privacyChannel.getChannel().showInter(true, style)


                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }

                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }
                break
        }
    }
}
